package dingding

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

type param struct {
	At struct {
		AtMobiles []string `json:"atMobiles "`
		AtUserIds []string `json:"atUserIds "`
		IsAtAll   bool     `json:"isAtAll "`
	} `json:"at"`
	Text struct {
		Content string `json:"content"`
	} `json:"text"`
	Msgtype string `json:"msgtype"`
}

func SendTextToDingding(url, content string, atUserIds, atMobiles []string, isAtAll bool) {
	p := param{
		Msgtype: "text",
		Text: struct {
			Content string `json:"content"`
		}{
			content,
		},
		At: struct {
			AtMobiles []string `json:"atMobiles "`
			AtUserIds []string `json:"atUserIds "`
			IsAtAll   bool     `json:"isAtAll "`
		}{
			AtMobiles: atMobiles,
			AtUserIds: atUserIds,
			IsAtAll:   isAtAll,
		},
	}
	pj, _ := json.Marshal(p)
	parame := bytes.NewReader(pj)
	resp, err := http.Post(url, "application/json", parame)
	if err != nil {
		fmt.Println("err : ", err)
		return
	}
	if resp.StatusCode != 200 {
		fmt.Println("status code : ", resp.StatusCode)
		return
	}
	by, _ := ioutil.ReadAll(resp.Body)
	fmt.Println("string:", string(by))
}
